package dviz.tools;

import java.net.URL;
import java.util.Collection;
import java.util.Map;

import javax.swing.JFrame;
import com.gibbon.jme.context.JmeContext;
import dviz.visualAspect.DeclarationTranslation;
import dviz.visualSystem.DummyAnimationSystem;
import dviz.visualSystem.impl.AnimationSceneSystem;
import dviz.visualSystem.impl.AnimationSystemImpl;
import dviz.visualSystem.impl.CameraControl;

/**
 * @author  zxq071000
 */
public class DiscoveryFrame extends JFrame {
	/**
	 * @uml.property  name="animationSceneSystem"
	 * @uml.associationEnd  
	 */
	AnimationSceneSystem animationSceneSystem;
	JmeContext context;
	/**
	 * @uml.property  name="cameraControl"
	 * @uml.associationEnd  
	 */
	CameraControl cameraControl;

	/**
	 * @uml.property  name="nodeSelector"
	 * @uml.associationEnd  
	 */
	NodeSelector nodeSelector;

	public DiscoveryFrame(AnimationSystemImpl systemImpl,
			ModelRepository modelRepository) {
		setSize(1024, 768);
		animationSceneSystem = new AnimationSceneSystem(modelRepository,
				systemImpl);

		context = ModelUtil.createCanvas(getWidth(), getHeight(),
				modelRepository.getModelContext());

		this.getContentPane().add(context.getCanvas());
		new Thread() {
			@Override
			public void run() {
				try {
					context.waitFor();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				animationSceneSystem.setCanvas(context);
			}
		}.start();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		nodeSelector = new NodeSelector();
//		nodeSelector.setVisible(true);
		nodeSelector.rootNode = systemImpl.getSystemRootNode();
		nodeSelector.parent=this;
		// cameraControl = new WholeViewCameraControl();
		// cameraControl.addListener(context.getCanvas());
		setCameraControl(nodeSelector.getCameraControl());
		
	}
	
	/**
	 * @param cameraControl
	 * @uml.property  name="cameraControl"
	 */
	public void setCameraControl(CameraControl cameraControl){
		if(this.cameraControl!=null)
			cameraControl.removeListener(context.getCanvas());
		this.cameraControl = cameraControl;
		cameraControl.addListener(context.getCanvas());
		animationSceneSystem.setCameraControl(cameraControl);
	}

	public static DiscoveryFrame createXMLInstance(String XMLFileName,
			String mainClass, String dataFolder, Map<String, String> loadModel,
			Collection<String> voPrefix) throws Exception {
		Splash splash = new Splash();
		final ModelRepository modelRepository = new ModelRepository();
		modelRepository.setDataFolder(dataFolder);
		modelRepository.setMonitor(splash);
		modelRepository.getLoadList().putAll(loadModel);
		modelRepository.loadModel();
		modelRepository.waitForLoad();

		final AnimationSystemImpl animationSystemImpl = AnimationSystemImpl
				.createAnimationSystem();
		for (String prefix : voPrefix) {
			animationSystemImpl.getAnimationClassLocator().addPrefix(prefix);
		}
		DiscoveryFrame discoveryFrame = new DiscoveryFrame(animationSystemImpl,
				modelRepository);
		discoveryFrame.setVisible(true);

		URL XMLFile = DiscoveryFrame.class.getClassLoader().getResource(
				XMLFileName);
		DeclarationTranslation declarationTranslation = DeclarationTranslation
				.createByXML(XMLFile);
		declarationTranslation.addAnimationSystem(animationSystemImpl);

		declarationTranslation.translateInvoke(mainClass, "main",
				new Object[] { new String[0] });
		return discoveryFrame;
	}

	public static DiscoveryFrame createInstance(String mainClass,
			Map<String, String> loadModel, Collection<String> voPrefix)
			throws Exception {
		Splash splash = new Splash();
		final ModelRepository modelRepository = new ModelRepository();
		modelRepository.setMonitor(splash);
		modelRepository.getLoadList().putAll(loadModel);
		modelRepository.loadModel();
		modelRepository.waitForLoad();

		final AnimationSystemImpl animationSystemImpl = AnimationSystemImpl
				.createAnimationSystem();
		for (String prefix : voPrefix) {
			animationSystemImpl.getAnimationClassLocator().addPrefix(prefix);
		}
		DiscoveryFrame discoveryFrame = new DiscoveryFrame(animationSystemImpl,
				modelRepository);
		discoveryFrame.setVisible(true);

		//
		DeclarationTranslation declarationTranslation = DeclarationTranslation
				.createByAnnotation();
		declarationTranslation.addAnimationSystem(animationSystemImpl);

		declarationTranslation.translateInvoke(mainClass, "main",
				new Object[] { new String[0] });
		return discoveryFrame;
	}

	public static void createDummySystem(String mainClass,
			Map<String, String> loadModel, Collection<String> voPrefix)
			throws Exception {
		DeclarationTranslation declarationTranslation = DeclarationTranslation
				.createByAnnotation();
		DummyAnimationSystem animationSystemImpl = new DummyAnimationSystem();
		declarationTranslation.addAnimationSystem(animationSystemImpl);

		declarationTranslation.translateInvoke(mainClass, "main",
				new Object[] { new String[0] });
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
	}
}
